<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2017/11/24
 * Time: 6:31
 */

namespace Application\Config\Controller;


use Application\Common\Controller\ElementUIController;
use J\Exception;
use J\Intent;

class DatabaseController extends ElementUIController
{
    protected static $databaseTypes = ['mysql', 'mssql'];

    /**
     * 生成配置界面
     * @param Intent $intent
     */
    protected function optionElementUIIndex(Intent $intent)
    {
        $intent->value = [
            'view' => [
                'name' => 'Module',
                'item' => [
                    'label' => '数据库配置',
                    'holder' => 'item-group',
                    'items' => [
                        'type' => [
                            'holder' => 'select',
                            'options' => [
                                ['label' => 'mysql', 'value' => 0],
                                ['label' => 'mssql', 'value' => 1]
                            ],
                            'name' => 'type',
                            'label' => '引擎',
                        ],
                        'host' => [
                            'name' => 'host',
                            'label' => '主机',
                        ],
                        'port' => [
                            'name' => 'port',
                            'label' => '端口',
                        ],
                        'user' => [
                            'name' => 'user',
                            'label' => '用户名',
                            'must' => true
                        ],
                        'pass' => [
                            'name' => 'pass',
                            'label' => '密码',
                            'holder' => 'password',
                            'must' => true
                        ],
                        'name' => [
                            'name' => 'name',
                            'label' => '数据库名称',
                            'must' => true
                        ],
                    ],
                    'asArray' => true,
                    'name' => 'Database',
                ],
                'value' => $this->getConfig(),
                'url' => $intent->makeUrl('Set', '/api.php'),
            ],
            'title' => '基础配置',
        ];
    }

    /**
     * 获取配置
     * @return array
     */
    protected function getConfig()
    {
        $config = $this->getModule()->getApp()->get('Database', []);

        foreach ($config as $c) {
            $c['type'] = (int)(array_search($c['type'], static::$databaseTypes));
        }

        return ['Database' => $config];
    }

    /**
     * 配置获取接口
     * @param Intent $intent
     */
    protected function optionApiIndex(Intent $intent)
    {
        $intent->value = $this->getConfig();
    }

    /**
     * 修改配置接口
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiSet(Intent $intent)
    {

        /** @var array $setting 获取提交信息 */
        $setting = $_REQUEST['Database'];
        $app = $this->getModule()->getApp();

        /** @var [] $db 获取原配置信息 */
        $db = $app->get('Database', [[]]);
        $newSetting = [];
        foreach ($setting as $i => $s) {
            $newSetting[$i] = array_pass([
                // 'type' => [null, 0],
                'host' => [null, 'localhost'],
                'port' => [null, 3306],
                'user' => null,
                'pass' => null,
                'name' => null
            ], $s, $db[$i]);
            $newSetting[$i]['type'] = array_isset(static::$databaseTypes, (int)($s['type']), static::$databaseTypes[0]);
        }

        $app->getConfig()->set($newSetting, 'Database');
        $app->getConfig()->save();

        // 安全起见，安装前必须创建好数据库
        $pdo = $app->Database()->getPdo();
        $pdo->getAttribute(\PDO::ATTR_SERVER_INFO);
        // 重建数据工作各模块自己完成
        $intent->ajax(true, '保存完成');
    }
}